package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.SensitiveDto;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmSensitiveService;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper,WmSensitive> implements WmSensitiveService {
    /**
     * 敏感词-列表查询
     * @param dto
     * @return
     */
    @Override
    public PageResponseResult findAll(SensitiveDto dto) {
        // 1 设置分页参数
        Page<WmSensitive> pageSearch = new Page<>(dto.getPage(), dto.getSize());
        // 2 封装查询条件
        LambdaQueryWrapper<WmSensitive> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(WmSensitive::getSensitives,dto.getName())
                .orderByDesc(WmSensitive::getCreatedTime);
        // 3 分页查询
        Page<WmSensitive> pageInfo = page(pageSearch, wrapper);
        // 4 封装返回对象
        PageResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), (int)pageInfo.getTotal());
        result.setData(pageInfo.getRecords());
        return result;
    }

    /**
     * 添加敏感词
     * @param wmSensitive
     * @return
     */
    @Override
    public ResponseResult addSensitive(WmSensitive wmSensitive) {
        LambdaQueryWrapper<WmSensitive> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WmSensitive::getSensitives,wmSensitive.getSensitives());
        WmSensitive one = getOne(wrapper);
        if (one == null) {
            if (wmSensitive.getCreatedTime() == null) {
                wmSensitive.setCreatedTime(new Date());
            }
            save(wmSensitive);
        } else {
            return ResponseResult.error(AppHttpCodeEnum.DATA_EXIST);
        }
        return ResponseResult.okResult();
    }

    /**
     * 删除敏感词
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteSensitive(Integer id) {
        removeById(id);
        return ResponseResult.okResult();
    }

    /**
     * 修改敏感词
     * @param wmSensitive
     * @return
     */
    @Override
    public ResponseResult updateSensitive(WmSensitive wmSensitive) {
        updateById(wmSensitive);
        return ResponseResult.okResult();
    }
}
